package org.hfy.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.hfy.dto.EmployeeQueryDTO;
import org.hfy.entity.Employee;
import org.hfy.mapper.EmployeeMapper;
import org.hfy.service.EmployeeService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 员工Service实现类
 * 
 * @author hfy
 * @version 1.0
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Override
    public IPage<Employee> getEmployeePage(EmployeeQueryDTO queryDTO) {
        Page<Employee> page = new Page<>(queryDTO.getCurrent(), queryDTO.getSize());
        return baseMapper.selectEmployeePage(page, queryDTO);
    }

    @Override
    public Employee getEmployeeById(Long id) {
        return this.getById(id);
    }

    @Override
    public boolean addEmployee(Employee employee) {
        // 检查员工代码是否已存在
        if (checkEmployeeCodeExists(employee.getEmployeeCode(), null)) {
            throw new RuntimeException("员工代码已存在");
        }
        
        // 设置默认值
        if (StrUtil.isBlank(employee.getStatus())) {
            employee.setStatus("active");
        }
        
        // 如果设置了密码，则进行MD5加密
        if (StrUtil.isNotBlank(employee.getPassword())) {
            employee.setPassword(DigestUtil.md5Hex(employee.getPassword()));
        } else {
            // 设置默认密码为123456
            employee.setPassword(DigestUtil.md5Hex("123456"));
        }
        
        return this.save(employee);
    }

    @Override
    public boolean updateEmployee(Employee employee) {
        // 检查员工代码是否已存在（排除当前记录）
        if (checkEmployeeCodeExists(employee.getEmployeeCode(), employee.getId())) {
            throw new RuntimeException("员工代码已存在");
        }
        
        // 如果更新了密码，则进行MD5加密
        if (StrUtil.isNotBlank(employee.getPassword())) {
            // 检查密码是否已经是MD5加密（32位16进制字符串）
            if (!employee.getPassword().matches("[0-9a-fA-F]{32}")) {
                employee.setPassword(DigestUtil.md5Hex(employee.getPassword()));
            }
        } else {
            // 如果密码为空，则不更新密码字段
            Employee oldEmployee = this.getById(employee.getId());
            employee.setPassword(oldEmployee.getPassword());
        }
        
        return this.updateById(employee);
    }

    @Override
    public boolean deleteEmployee(Long id) {
        return this.removeById(id);
    }

    @Override
    public boolean checkEmployeeCodeExists(String employeeCode, Long excludeId) {
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("employee_code", employeeCode);
        if (excludeId != null) {
            queryWrapper.ne("id", excludeId);
        }
        return this.count(queryWrapper) > 0;
    }
    
    @Override
    public List<Employee> getAllEmployees() {
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", false);
        queryWrapper.orderByAsc("employee_code");
        return this.list(queryWrapper);
    }
} 